Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Data Views] Remove remaining "any" usage #136343

Merged
merged 32 commits into from
Jul 19, 2022

Conversation

tsullivan
Copy link
Member

@tsullivan tsullivan commented Jul 13, 2022

Summary

Follows #135767

This removes the remaining any from production code and code comments in src/plugins/data_v*

Note: some instances have remained due to ambiguity in the code, but these have had added FIXME comments:

After this PR, the remaining matches of the term any exist in the following:

  • the various added FIXMEs
  • unit test code
  • user-facing content strings that includes the string any

Remaining any matches:
image

@tsullivan tsullivan force-pushed the dataviews/remove-any-usage-ii branch from 4acfa7d to bf3d887 Compare July 13, 2022 20:58
@tsullivan tsullivan marked this pull request as ready for review July 14, 2022 01:16
@tsullivan tsullivan requested review from a team as code owners July 14, 2022 01:16
@tsullivan tsullivan added Feature:Data Views Data Views code and UI - index patterns before 8.0 release_note:skip Skip the PR/issue when compiling release notes backport:skip This commit does not require backporting Team:AppServicesUx v8.4.0 labels Jul 14, 2022
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-app-services (Team:AppServicesUx)

@tsullivan tsullivan marked this pull request as draft July 14, 2022 01:19
Copy link
Contributor

@nreese nreese left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kibana-gis changes LGTM
code review

@tsullivan tsullivan marked this pull request as ready for review July 14, 2022 14:17
waitInMs: number
): ((key: string) => F) => {
const debouncerCollector: Record<string, F> = {};
export const debounceByKey = <F extends (...args: any) => unknown>(fn: F, waitInMs: number) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there's a way to get rid of this any

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be good to leave a code comment

} from './lib';
export type { RollupIndexCapability } from './lib';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be exported since its part of a public API

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you document it? Trying to prevent Public APIs missing comments from increasing

const isMultiple = intervals[1] % intervals[0] === 0;
fieldAgg.interval = isMultiple ? intervals[1] : intervals[0] * intervals[1];
if (fieldAgg) {
const aggInterval = (agg as any).interval ?? agg.calendar_interval; // FIXME the interface infers only that calendar_interval property is valid
Copy link
Member Author

@tsullivan tsullivan Jul 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find an interval field on the interface that is expected here, I thought maybe it should be calendar_interval -- or maybe the typings are not complete for the histogram agg

@@ -7,7 +7,7 @@

import type { IndexPatternAggRestrictions } from '@kbn/data-plugin/public';
import type { FieldSpec } from '@kbn/data-plugin/common';
import type { FieldFormatParams } from '@kbn/field-formats-plugin/common';
import type { FieldFormatMap } from '@kbn/data-views-plugin/common';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why FieldFormat and FieldFormatParams are exported from the field-formats plugin,

but FieldFormatMap is exported from the data-views plugin. 😕

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FieldFormatMap is how data views saves field formatters

@@ -92,7 +93,7 @@ export function convertDataViewIntoLensIndexPattern(dataView: DataView): IndexPa
Object.fromEntries(
Object.entries(fieldFormatMap).map(([id, format]) => [
id,
'toJSON' in format ? format.toJSON() : format,
'toJSON' in format ? (format as unknown as FieldFormat).toJSON() : format, // FIXME SerializedFieldFormat was inferred... was this intended to work with FieldFormat instead?
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need reviewers to look closely at this: I could not see how there would be a .toJSON property on this object.

Copy link
Contributor

@mattkime mattkime left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of notes otherwise great work!

*/
export function shortenDottedString(input: any) {
return typeof input !== 'string' ? input : input.replace(DOT_PREFIX_RE, '$1.');
export function shortenDottedString(input: unknown): string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When is this called on something aside from a string?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was only in unit tests

@@ -432,7 +432,7 @@ function FieldItemPopoverContents(props: State & FieldItemProps) {

let formatter: { convert: (data: unknown) => string };
if (indexPattern.fieldFormatMap && indexPattern.fieldFormatMap[field.name]) {
const FormatType = fieldFormats.getType(indexPattern.fieldFormatMap[field.name].id);
const FormatType = fieldFormats.getType(indexPattern.fieldFormatMap[field.name].id as string);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps fieldFormats.getType needs better type info instead of using as string on the id

Copy link
Member Author

@tsullivan tsullivan Jul 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change in this PR is to type-guard that indexPattern.fieldFormatMap[field.name].id is a string, because an argument to FieldFormatsRegistry.getType is required: https://github.com/elastic/kibana/blob/8.3/src/plugins/field_formats/common/field_formats_registry.ts#L92

Hm, checking against main, the "fieldFormats" object was typed as FieldFormatsStart not FieldFormatsRegistry. Need to look into this.

Edit: this change came from the type refinement where indexPattern.fieldFormatMap was typed as:

(property) IndexPattern.fieldFormatMap?: Record<string, {
    id: string;
    params: SerializableRecord;
}>

But is now: IndexPattern.fieldFormatMap?: FieldFormatMap, which types to Record<string, SerializedFieldFormat>, and types to this:

export type SerializedFieldFormat<
  P = {},
  TParams extends FieldFormatParams<P> = FieldFormatParams<P>
> = {
  id?: string;
  params?: TParams;
};

With the refined types, id is now inferred to be optional. The previous typing from this code assumed id and params is not optional: https://github.com/elastic/kibana/blob/8.3/x-pack/plugins/lens/public/indexpattern_datasource/types.ts#L59-L60

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have zero understanding what consequences this might bring but it would be nice if id could be required. I guess params too if that works with existing code. Perhaps an empty object is always provided.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the object could come from user input, and the ID is not validated to be present: https://github.com/elastic/kibana/blob/8.3/src/plugins/data_views/server/rest_api_routes/util/schemas.ts#L13.

There is quite a lot of test code that relies on these fields being optional as well.

@tsullivan
Copy link
Member Author

It looks like the code metric script finds that DataViewSavedObjectAttrs is an uncommented public API. I'm not sure why it's doing that, since the needed comment seems to be there.

@cla-checker-service
Copy link

cla-checker-service bot commented Jul 15, 2022

💚 CLA has been signed

@tsullivan tsullivan force-pushed the dataviews/remove-any-usage-ii branch from 3cca8c7 to 250d034 Compare July 15, 2022 20:49
@tsullivan tsullivan force-pushed the dataviews/remove-any-usage-ii branch from c9de913 to 2a2d14a Compare July 15, 2022 20:53
@@ -100,6 +100,7 @@ export function convertDataViewIntoLensIndexPattern(dataView: DataView): IndexPa
Object.fromEntries(
Object.entries(fieldFormatMap).map(([id, format]) => [
id,
// @ts-expect-error FIXME Property 'toJSON' does not exist on type 'SerializedFieldFormat'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dej611 I remember we were struggling a bit with this - do you know where this is coming from? How can there be a toJSON on the format that should be serialized? Was this done to work around some bug?

Copy link
Contributor

@flash1293 flash1293 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VisEditors changes LGTM

Copy link
Contributor

@mattkime mattkime left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changes lgtm - nice type improvements!

@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

Public APIs missing comments

Total count of every public API that lacks a comment. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats comments for more detailed information.

id before after diff
dataViews 202 203 +1

Any counts in public APIs

Total count of every any typed public API. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats any for more detailed information.

id before after diff
dataViews 1 0 -1

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
visTypeTimeseries 452.0KB 452.0KB +57.0B

Public APIs missing exports

Total count of every type that is part of your API that should be exported but is not. This will cause broken links in the API documentation system. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats exports for more detailed information.

id before after diff
dataViews 1 0 -1

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
dataViews 40.7KB 40.8KB +83.0B
Unknown metric groups

API count

id before after diff
dataViews 942 945 +3

ESLint disabled line counts

id before after diff
dataViews 10 9 -1

Total ESLint disabled count

id before after diff
dataViews 12 11 -1

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@tsullivan tsullivan merged commit a2c3323 into elastic:main Jul 19, 2022
@tsullivan tsullivan deleted the dataviews/remove-any-usage-ii branch July 29, 2022 23:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip This commit does not require backporting Feature:Data Views Data Views code and UI - index patterns before 8.0 release_note:skip Skip the PR/issue when compiling release notes v8.4.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants